package cn.piesat.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.springframework.data.domain.Page;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 接口统一返回格式
 * @author Administrator
 * @date 2019-5-22
 */
public class ResponseWrapperUtil {
 
    /**
     * 用于保存,更新,删除提示
     * @param state
     * @return
     */
    public static String executeResultTips(boolean state) {
    	Map<Object,Object> jsonResult = new HashMap<Object,Object>();
    	jsonResult.put("success", state==true?"true":"false");
    	jsonResult.put("code", state==true? ReturnCodeEnum.SUCCESS.getCode():ReturnCodeEnum.FEAILED.getCode());
    	jsonResult.put("msg", state==true?ReturnCodeEnum.SUCCESS.getMsg():ReturnCodeEnum.FEAILED.getMsg());
    	return JSON.toJSONString(jsonResult);
    }
    
    /**
     * 用户返回List集合类型数据
     * @param obj
     * @return
     */
    public static String executeResultList(List<?> obj) {
    	Map<Object,Object> jsonResult = new HashMap<Object,Object>();
    	if(obj.size() <= 0) {
    		jsonResult.put("success", "true");
        	jsonResult.put("code", ReturnCodeEnum.NODATA.getCode());
        	jsonResult.put("msg", ReturnCodeEnum.NODATA.getMsg());
    	}else {
    		jsonResult.put("success", "true");
        	jsonResult.put("code", ReturnCodeEnum.SUCCESS.getCode());
        	jsonResult.put("msg", ReturnCodeEnum.SUCCESS.getMsg());
        	jsonResult.put("data", obj);
    	}
    	return JSON.toJSONString(jsonResult);
    }
    
    /**
     * 用于返回Page类型数据
     * @param obj
     * @return
     */
    public static String executeResultPageMap(Page<?> obj) {
    	Map<Object,Object> jsonResult = new HashMap<Object,Object>();
    	if(obj == null) {
    		jsonResult.put("success", "true");
        	jsonResult.put("code", ReturnCodeEnum.NODATA.getCode());
        	jsonResult.put("msg", ReturnCodeEnum.NODATA.getMsg());
    	}else {
    		jsonResult.put("success", "true");
        	jsonResult.put("code", ReturnCodeEnum.SUCCESS.getCode());
        	jsonResult.put("msg", ReturnCodeEnum.SUCCESS.getMsg());
        	jsonResult.put("data", obj);
    	}
    	return JSON.toJSONString(jsonResult);
    }
    
    /**
     * 用于返回Map类型数据
     * @param obj
     * @return
     */
    public static String executeResultMap(Map<?,?> obj) {
    	Map<Object,Object> jsonResult = new HashMap<Object,Object>();
    	if(obj.size() <= 0) {
    		jsonResult.put("success", "true");
        	jsonResult.put("code", ReturnCodeEnum.NODATA.getCode());
        	jsonResult.put("msg", ReturnCodeEnum.NODATA.getMsg());
    	}else {
    		jsonResult.put("success", "true");
        	jsonResult.put("code", ReturnCodeEnum.SUCCESS.getCode());
        	jsonResult.put("msg", ReturnCodeEnum.SUCCESS.getMsg());
        	jsonResult.put("data", obj);
    	}
    	return JSON.toJSONString(jsonResult);
    }
    
    /**
     * 用于返回Map类型数据
     * @param obj
     * @return
     */
    public static String executeResultMap(Map<?,?> obj, Map<?,?> permission) {
    	Map<Object,Object> jsonResult = new HashMap<Object,Object>();
    	if(obj.size() <= 0) {
    		jsonResult.put("success", "true");
        	jsonResult.put("code", ReturnCodeEnum.NODATA.getCode());
        	jsonResult.put("msg", ReturnCodeEnum.NODATA.getMsg());
    	}else {
    		jsonResult.put("success", "true");
        	jsonResult.put("code", ReturnCodeEnum.SUCCESS.getCode());
        	jsonResult.put("msg", ReturnCodeEnum.SUCCESS.getMsg());
        	jsonResult.put("data", obj);
        	if(permission.size() > 0) {
        		jsonResult.put("permission", permission);        		
        	}
    	}
    	return JSON.toJSONString(jsonResult);
    }
    
    /**
     * 用于返回实体类数据
     * @param obj
     * @return
     */
    public static String executeResultEntity(Object obj) {
    	Map<Object,Object> jsonResult = new HashMap<Object,Object>();
		jsonResult.put("success", "true");
    	jsonResult.put("code", ReturnCodeEnum.SUCCESS.getCode());
    	jsonResult.put("msg", ReturnCodeEnum.SUCCESS.getMsg());
    	jsonResult.put("data", obj);
    	return JSON.toJSONString(jsonResult,SerializerFeature.WriteNullStringAsEmpty);
    }
    
    /**
     * 用于代码执行出现异常,响应请求返回
     * @return
     */
    public static String executeCodeException() {
    	Map<Object,Object> jsonResult = new HashMap<Object,Object>();
    	jsonResult.put("success", "false");
    	jsonResult.put("code", ReturnCodeEnum.SYSTEM_ERROR.getCode());
    	jsonResult.put("msg", ReturnCodeEnum.SYSTEM_ERROR.getMsg());
    	return JSON.toJSONString(jsonResult);
    }
    
    /**
     * 自定义消息提示，需要传入状态和提示内容
     * @param state 状态
     * @param msg 提示内容
     * @return
     */
    public static String executeCustomTips(String state,String msg) {
    	Map<Object,Object> jsonResult = new HashMap<Object,Object>();
    	jsonResult.put("success", state);
    	if(state.equals("true")) {
    		jsonResult.put("code", ReturnCodeEnum.SUCCESS.getCode());
    	}else {
    		jsonResult.put("code", ReturnCodeEnum.CUSTOM.getCode());    		
    	}
    	jsonResult.put("msg", msg);
    	return JSON.toJSONString(jsonResult);
    }
    
    /**
     * 用于返回单个字符串数据
     * @param strMsg 返回的字符串
     * @return
     */
    public static String executeOneCharString(String strMsg) {
    	Map<Object,Object> jsonResult = new HashMap<Object,Object>();
    	Map<Object,Object> dataResult = new HashMap<Object,Object>();
    	dataResult.put("result", strMsg);
    	if(strMsg!=null && !strMsg.equals("")) {
    		jsonResult.put("success", "true");
    		jsonResult.put("code", ReturnCodeEnum.SUCCESS.getCode());
    		jsonResult.put("msg", ReturnCodeEnum.SUCCESS.getMsg());
    		jsonResult.put("data", dataResult);
    	}else {
    		jsonResult.put("success", "false");
    		jsonResult.put("code", ReturnCodeEnum.FEAILED.getCode());
    		jsonResult.put("msg", ReturnCodeEnum.FEAILED.getMsg());
    	}
    	return JSON.toJSONString(jsonResult);
    }

	/**
	 * 参数错误时返回的信息
	 * @return
	 */
	public static String executeParamError(){
		Map<Object,Object> jsonResult = new HashMap<Object,Object>();
		jsonResult.put("success", "false");
		jsonResult.put("code", ReturnCodeEnum.PARAMS_ERROR.getCode());
		jsonResult.put("msg", ReturnCodeEnum.PARAMS_ERROR.getMsg());
		return JSON.toJSONString(jsonResult);
	}

	/**
	 * 自定义信息和返回码
	 * @param code 返回码
	 * @param msg	信息
	 * @param success	是否成功
	 * @param data	数据
	 * @return
	 */
	public static String executeCodeString(String code,String msg,boolean success,Object data) {
		Map<Object,Object> jsonResult = new HashMap<Object,Object>();
		if(data == null) {
			jsonResult.put("code", code);
			jsonResult.put("msg", msg);
			jsonResult.put("success", JSON.toJSONString(success));
		} else {
			jsonResult.put("code", code);
			jsonResult.put("msg", msg);
			jsonResult.put("success", JSON.toJSONString(success));
			jsonResult.put("data", data);
		}
		return JSON.toJSONString(jsonResult,SerializerFeature.DisableCircularReferenceDetect);
	}

	public static String resultCustomEnumString(ReturnCodeEnum returnCodeEnum, boolean success) {
		Map<Object,Object> jsonResult = new HashMap<Object,Object>();
		jsonResult.put("code", returnCodeEnum.getCode());
		jsonResult.put("msg", returnCodeEnum.getMsg());
		jsonResult.put("success", JSON.toJSONString(success));
		return JSON.toJSONString(jsonResult,SerializerFeature.DisableCircularReferenceDetect);
	}
}
